package com.lyb.desaes3desencrption;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.TextView;

import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

//非对称加密
//加密和解密两套密码
public class RSAActivity extends AppCompatActivity {
    private TextView showInfo;
    private PublicKey mPublicKey;
    private PrivateKey mPrivateKey;
    private byte[] mEncode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rsa);
        initView();
    }

    private void initView() {
        showInfo = (TextView) findViewById(R.id.pass);
    }

    //生成公钥和私钥 要耗费时间，开启子线程
    public void click(View view) {
        //加密的内容
        String check = "11111,22222";
        switch (view.getId()) {
            case R.id.generate:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            //1、生成公钥和私钥生成的对象
                            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
                            //2、初始化，密码的长度 bit
                            keyPairGenerator.initialize(1024);
                            //3、获取公钥和私钥
                            KeyPair keyPair = keyPairGenerator.generateKeyPair();
                            //3.1获取私钥 (非常重要，保存好，丢了就没有)
                            mPrivateKey = keyPair.getPrivate();
                            //3.2获取公钥  (丢了可以通过私钥获得)
                            mPublicKey = keyPair.getPublic();
                            //私钥的具体数据，字节数组，保存该数据
                            //必须Base64转换！！！！！！！
                            byte[] mPrivateKeyEncoded = mPrivateKey.getEncoded();
                            byte[] mPublicKeyEncoded = mPublicKey.getEncoded();
                            final byte[] encodePrivate = Base64.encode(mPrivateKeyEncoded, Base64.DEFAULT);
                            final byte[] encodePublic = Base64.encode(mPublicKeyEncoded, Base64.DEFAULT);
                            showInfo.post(new Runnable() {
                                @Override
                                public void run() {
                                    showInfo.setText("私钥是:" + new String(encodePrivate) + "\n" + "公钥是:" + new String(encodePublic));
                                }
                            });
                        } catch (NoSuchAlgorithmException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
            case R.id.enRSA://加密
                //使用私钥去加密，使用公钥解密
                try {
                    //1、获取加密引擎
                    Cipher cipher = Cipher.getInstance("RSA");
                    //2、初始化
                    if (mPrivateKey != null) {
                        cipher.init(Cipher.ENCRYPT_MODE,mPrivateKey);
                    }
                    //3、加密
                    byte[] dofinal = cipher.doFinal(check.getBytes());
                    mEncode = Base64.encode(dofinal, Base64.DEFAULT);
                    showInfo.setText("RSA加密="+new String(mEncode));
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.deRSA://解密
                try {
                    //1、获取解密引擎
                    Cipher cipher = Cipher.getInstance("RSA");
                    //2、初始化
                    if (mPrivateKey != null) {
                        cipher.init(Cipher.DECRYPT_MODE,mPublicKey);
                    }
                    //3、解密
                    byte[] encode = Base64.decode(mEncode,Base64.DEFAULT);
                    byte[] dofinal = cipher.doFinal(encode);
                    showInfo.setText("RSA解密="+new String(dofinal));
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                }
                break;
        }

    }
}
